import variable from '@/data/variable.json'
import columns from '@/data/columns.json'
import formEditBox from '@/data/formEditBox.json'
import formPostUrl from '@/data/formPostUrl.json'
export default {
	name: 'tag',
	components: {},
	// watch: {
	// 	'$route'(to, from) {
	// 		console.log(123);
	// 	}
	// },
	data() {
		return {
			columns: columns[this.$route.name] ? columns[this.$route.name] : null,
			dictionary: variable.dictionary,
			deleteItem: null,
			data: null,
			resType: 0,
			pageNum: 1,
			limit: 10,
			total: 0,
			totalPage: 0,
			list: [],
			listView: [],
			// 搜索栏数据
			formSearch: {
				input: '',
			},
			// 编辑框是否打开
			editBox: false,
			// 编辑框数据
			formEditBox: formEditBox[this.$route.name].value ? formEditBox[this.$route.name].value : null,
			formEditBoxInfo: formEditBox[this.$route.name].info ? formEditBox[this.$route.name].info : null,
			tableAction: {
				title: '操作',
				key: 'action',
				fixed: 'right',
				minWidth: 130,
				render: (h, params) => {
					return h('div', {
						'class': 'm-my-1 m-ta-c'
					}, [
						h('Button', {
							props: {
								type: 'info',
								size: 'small',
								ghost: true,
							},
							'class': 'm-mr-2',
							on: {
								click: () => {
									this.editBoxOpen(params.index)
								}
							}
						}, '修改'),
						h('Button', {
							props: {
								type: 'error',
								size: 'small',
								ghost: this.deleteItem === params.index ? false : true,
							},
							on: {
								click: () => {
									if (this.deleteItem !== params.index) {
										this.deleteItem = params.index
									} else {
										this.delItem(this.list[this.deleteItem].id)
									}
								}
							}
						}, this.deleteItem === params.index ? '确定' : '删除'),
						// h('Button', {
						// 	props: {
						// 		type: 'default',
						// 		size: 'small'
						// 	},
						// 	'class': 'm-mt-1'
						// }, '查看关联商品')
					]);
				}
			},
		}
	},
	methods: {
		pageLoad(name) {
			console.log(formPostUrl[this.$route.name].loadList)
			this.loading = true
			this.axios.post(formPostUrl[this.$route.name].loadList,
				this.resType === 2 ? {
					pageNum: this.pageNum,
					pageSize: this.limit,
					name: name === '' ? null : name,
				} : {
					start: this.pageNum,
					limit: this.limit,
					name: name === '' ? null : name,
				}
			).then((response) => {
				let res = response.data
				if (res.code === 0 || res.code === '200') {
					if (res.code === 0) {
						this.resType = 1
					} else if (res.code === '200') {
						this.resType = 2
					}

					// 获取并替换显示内容
					let data
					if (res.data) {
						// 数据
						data = res.data.datas
						// 页数
						this.total = res.data.total
						this.totalPage = res.data.totalPage
					} else if (res.result) {
						// 数据
						data = res.result.list
						// 页数
						this.total = res.result.total
						this.totalPage = res.result.pages
					}
					// 数据处理与赋值
					this.list = data
					this.listView = data.map(item => _.mapValues(item, (value, key) => _.get(this.dictionary, key + '.' + value, value)))
				} else {
					this.$Notice.error({
						title: '错误',
						desc: res.message
					});
				}
			})
		},
		pageReload(name, pageNum) {
			console.log('页面重载:', name);
			this.pageNum = pageNum ? pageNum : 1
			this.pageLoad(name)
		},
		pageChange(num) {
			// 改变页码
			this.pageNum = num
			this.pageLoad()
		},
		pageSizeChange(num) {
			// 改变你每页显示条数
			this.limit = num
			this.pageLoad()
		},
		editBoxOpen(num) {
			// 打开编辑框
			this.editBox = true
			if (num !== null && num !== undefined) {
				this.formEditBox = this.list[num]
			} else {
				this.editBoxClear()
			}
		},
		editBoxClose() {
			// 关闭编辑框
			this.editBox = false
			this.editBoxClear()
		},
		editBoxClear() {
			// 清空editBox
			for (let o in this.formEditBox) {
				this.formEditBox[o] = null
			}
		},
		editBoxSubmit() {
			if (!formPostUrl[this.$route.name] || !formPostUrl[this.$route.name].editItem || !formPostUrl[this.$route.name].addItem) {
				return false
			}
			// 编辑框提交
			let hasid = t.formEditBox.id ? true : false
			this.axios.post(
				hasid ? formPostUrl[this.$route.name].editItem : formPostUrl[this.$route.name].addItem,
				hasid ? this.formEditBox : _.omit(t.formEditBox, ['id'])
			).then((response) => {
				let res = response.data
				if (res.code === 0) {
					// 刷新列表
					this.pageReload('', this.pageNum)
					// 关闭编辑框
					this.editBoxClose()
				} else {
					this.$Notice.error({
						title: '错误',
						desc: res.message
					});
				}
			})
		},
		delItem(num) {
			if (!formPostUrl[this.$route.name] || !formPostUrl[this.$route.name].delItem) {
				console.log('111')
				return false
			}
			this.axios.post(formPostUrl[this.$route.name].delItem + '/' + num).then((response) => {
				let res = response.data
				if (res.code === 0) {
					// 刷新列表
					this.pageReload('', this.pageNum)
					this.$Notice.success({
						title: '删除成功',
						desc: '您已成功删除: <b>' + this.list[this.deleteItem].name + '</b>'
					});
					this.deleteItem = null
				} else {
					this.$Notice.error({
						title: '错误',
						desc: res.message
					});
				}
			})
		}
	},
	mounted() {
		window.t = this
		this.pageLoad()
		// 没找到列表action的话则添加
		if (!_.findKey(this.columns, 'render')) {
			this.columns.push(this.tableAction)
		}
	},
}
